package org.myself.meeting.controller;


import org.myself.meeting.Utils.Constant;
import org.myself.meeting.model.*;
import org.myself.meeting.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/managerboss")
public class ManagerBoss {
    @Autowired
    EmployeeService employeeService;

    @Autowired
    MeetingService meetingService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    AwardService awardService;

    @Autowired
    AbsenceService absenceService;

    @Autowired
    FileOperationService fileOperationService;

    //后台管理登录 _______________________________________________________________
//    @RequestMapping("/login")
//    public String register(){
//        return "managerbosslogin";
//    }

    @PostMapping("dologin")
    @ResponseBody
    public Result  dologin(@RequestBody BossLogin login,HttpSession httpsession){
        Employee employee = employeeService.doLogin(login.getUsername(),login.getPassword());
        if(employee == null){
            return new Result(400,"用户名或者密码错误",null);
        }else{
            if (employee.getRole() != Constant.FOURROlE){  //权限问题  超级管理员
                return new Result(400,"权限不够",null);
            }else{
                httpsession.setAttribute("currentuser",employee);
                return new Result(200,"success",null);
            }
        }
    }


    //后台管理主页面 (默认是人员管理)_______________________________________________________________
    @RequestMapping("/adminpeople")
    @ResponseBody
    public Result adminPeople(HttpSession httpSession){
        return new Result(200,"success",employeeService.getAllEmpsAdmin());
    }
    //编辑页面
    @RequestMapping("/editpeople")
    @ResponseBody
    public Result editPeople(@RequestBody Integer id,HttpSession httpSession){
        Employee emp = employeeService.getEmpById(id);
        return new Result(200,"success",emp);
    }

    //编辑确认页面
    @RequestMapping("/editsubmitpeople")
    @ResponseBody
    public Result editSubmitPeople(@RequestBody Employee emp){
        Integer result = employeeService.updateEmpAll(emp);
        return new Result(200,"success",null);
    }

    //删除用户
    @RequestMapping("/deletepeople")
    @ResponseBody
    public Result deletePeople(@RequestParam("id") Integer id){
        Integer result = employeeService.deleteEmpById(id);
        return new Result(200,"success",null);
    }

    //后台会议管理 _______________________________________________________________
    @RequestMapping("/adminmeeting")
    @ResponseBody
    public Result adminMeeting(HttpSession httpSession){
        List<Meeting> mts = meetingService.getAllmeetingsAdmin();
        return new Result(200,"success",mts);
    }
    //编辑会议页面
    @RequestMapping("/editmeeting")
    @ResponseBody
    public Result editMeeting(@RequestBody Integer id,HttpSession httpSession){
        Meeting mt = meetingService.getMeetingById(id);

        return new Result(200,"success",mt);
    }

    //编辑会议的确认页面
    @RequestMapping("/editsubmitmeeting")
    @ResponseBody
    public Result editSubmitMeeting(@RequestBody Meeting meeting){
        System.out.println(meeting);
        meetingService.updateDepAll(meeting);
        return new Result(200,"success",null);
    }

    //删除会议
    @RequestMapping("/deletemeeting")
    @ResponseBody
    public Result deleteMeeting(@RequestBody  Deleteingmeeting acc){
        meetingService.deleteMetById(acc.getId());
        return new Result(200,"success",null);
    }


    //后台部门管理 _______________________________________________________________
    @RequestMapping("/admindep")
    @ResponseBody
    public Result adminDep(HttpSession httpSession){
        List<Department> deps = departmentService.getAllDeps();
        return new Result(200,"success",deps);
    }

    //删除部门
    @RequestMapping("/deletedep")
    @ResponseBody
    public Result deleteDep(@RequestBody DeleteDep dep,HttpSession session){
//        Employee emp = (Employee) session.getAttribute("currentuser");
        departmentService.deleteDepartment(dep.getId(),dep.getDepartmentname());
        return new Result(200,"success",null);
    }

    //后台云盘管理 _________________________________________________________________

    //云文件删除界面
    @RequestMapping("/filedelete")
    @ResponseBody
    public Result fileDelete(HttpSession httpSession){
        return new Result(200,"success",fileOperationService.getAllFiles());
    }
    //云文件的删除
    @RequestMapping("/filedeletedo")
    @ResponseBody
    public Result fileDeleteDo(@RequestBody Integer id,HttpSession httpSession){
        Integer result = fileOperationService.deleteFileById(id);
        if(result == 1){
            return new Result(200,"success",null);
        }else{
            return new Result(400,"fail",null);
        }

    }



    //后台考勤管理 _______________________________________________________________

    @RequestMapping("/signin")
    @ResponseBody
    public Result SignIn(HttpSession httpSession){
        return new Result(200,"success",absenceService.getAllSignIn());
    }


    //删除考勤记录
    @GetMapping("/deletesignin")
    @ResponseBody
    public Result deleteSignIn(@RequestBody Integer id,HttpSession httpSession){
        System.out.println(id);
        Integer result = absenceService.deleteSignInById(id);
        if(result == 1){
            return new Result(200,"success",null);
        }else{
            return new Result(400,"fail",null);
        }

    }

    //后台娱乐功能________________________________________________________________
    //发放抽奖次数页面
    @RequestMapping("/getawardtime")
    @ResponseBody
    public Result getAwardTime(HttpSession httpSession){

        return new Result(200,"success",departmentService.getAllDeps());
    }

    //发放抽奖次数
    @RequestMapping("/updateawardtime")
    @ResponseBody
    public Result updateAwardTime(@RequestBody Deleteingmeeting acc){
        Integer result = employeeService.updateAwardtimesByDepId(acc.getId());
        return new Result(200,"success",null);
    }

    //抽奖商品的管理managerbossupdateaward
    @RequestMapping("/updateaward")
    @ResponseBody
    public Result updateAward(HttpSession httpSession){
        return new Result(200,"success",awardService.getAwardAll());
    }


    //编辑抽奖商品
    @RequestMapping("/editaward")
    @ResponseBody
    public Result editAward(@RequestBody Editaward ard){
        Integer result = awardService.updateAwarding(ard.getId(),ard.getName());
        if(result > 0){
            return new Result(200,"success",null);
        }
        return new Result(400,"fail",null);
    }



}
